package cn.bgs.controller;

import cn.bgs.pojo.Classroom;
import cn.bgs.service.ClassroomService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class ClassroomController {
    @Autowired private ClassroomService classroomService;

    @ResponseBody
    @RequestMapping("/findAllClassroom")
    public PageInfo<Classroom> findAllClassroom(@RequestBody String jsonStr, Model m){

        JSONObject jsonObject=JSONObject.parseObject(jsonStr);

        Integer pageNum=Integer.parseInt(jsonObject.getString("pageNum"));
        Integer pageSize = 4;
        System.err.println("pageNum:"+pageNum);
        PageInfo<Classroom> list = classroomService.findAllClassroom(pageNum, pageSize);
        m.addAttribute("p", list);
        return list;
    }
    @RequestMapping("/toClassroom")
    public String toClassroom(){
        return "classroom";
    }

    @ResponseBody
    @RequestMapping("/deleteClassroom")
    public String deleteClassroom(Integer pkId){

        classroomService.deleteClassroom(pkId);


        return "redirect:/findAllClassroom";

    }



    @RequestMapping("/toModify/{pkId}")
    public String toModify(@PathVariable(value ="pkId")Integer pkId, Model m){
        m.addAttribute("pkId",pkId);
        return "classroomModify";
    }

    @RequestMapping("/updateByIdClassroom")
    @ResponseBody
    public Map<String,Object> updateByIdClassroom(@RequestBody Classroom classroom){
        Map<String,Object> m = new HashMap<>();
        Classroom an=classroomService.selectByPrimaryKey(classroom.getPkId());
        m.put("an",an);

        //map集合转换为JSON对象 /需要一个pom 文件 fastjson 处理字符串
        JSONObject result = JSONObject.parseObject(JSON.toJSONString(m));
        //将JSON对象传递给前端AJAX接收
        return result;
    }

    @RequestMapping("/classroomUpdate")
    @ResponseBody
    public int  classroomUpdate(@RequestBody Classroom record){

       int ff= classroomService.updateByPrimaryKeySelective(record);

            return ff;
    }
    @RequestMapping("/classroomTz")
    public String classroomTz(){

        return "classroom";
    }

    @RequestMapping("addTzClassroom")
    public String addTzClassroom(){

        return "classroomAdd";
    }



    @RequestMapping("/classroomAdd")
    @ResponseBody
    public int classroomAdd(@RequestBody Classroom record){

        int add=classroomService.insertSelective(record);

        return add;
    }





}
